import { request } from '@umijs/max';
import { setAuthHeader } from '../utils/authInterceptor';

/**
 * 获取当前登录用户信息
 * @returns 用户信息
 */
export async function getUserInfo() {
  const headers: Record<string, string> = {
    'Content-Type': 'application/json',
  };
  
  // 添加认证头
  setAuthHeader(headers);
  
  return request('/api/user/info', {
    method: 'GET',
    headers,
  });
}

/**
 * 更新用户信息
 * @param params 用户信息参数
 * @returns 更新结果
 */
export async function updateUserInfo(params: {
  name?: string;
  nickName?: string;
  className?: string;
  stuNo?: string;
  phone?: string;
  email?: string;
  role?: string;
  gender?: number;
}) {
  const headers: Record<string, string> = {
    'Content-Type': 'application/json',
  };
  
  // 添加认证头
  setAuthHeader(headers);
  
  return request('/api/user/update', {
    method: 'PUT',
    headers,
    data: params,
  });
}

/**
 * 获取所有用户列表（需要管理员权限）
 * @returns 用户列表
 */
export async function getUserList() {
  const headers: Record<string, string> = {
    'Content-Type': 'application/json',
  };
  
  // 添加认证头
  setAuthHeader(headers);
  
  return request('/api/user/pagedList', {
    method: 'POST',
    headers,
  });
}

/**
 * 删除用户（需要管理员权限）
 * @param id 用户ID
 * @returns 删除结果
 */
export async function deleteUser(id: number) {
  const headers: Record<string, string> = {
    'Content-Type': 'application/json',
  };
  
  // 添加认证头
  setAuthHeader(headers);
  
  return request(`/api/user/delete/${id}`, {
    method: 'DELETE',
    headers,
  });
}